<script setup>
	import {
		inject,
		reactive,
		nextTick,
		provide,
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		useStore
	} from 'vuex'
	// #ifdef H5
	import chatCard from '@/components/chatCard/chatCard.js'
	// #endif
	import {
		onLaunch
	} from '@dcloudio/uni-app'
	import base from '@/api/baseUrl.js'
	const store = useStore()
	const userinfo = uni.getStorageSync('userinfo')
	const socket = inject("$socket")
	const baseUrl = base
	const baseUrlList = {
		'development': 'rtmp://192.168.1.156',
		'production': 'rtmp://boo.nat300.top',
	}
	const liveUrl = baseUrlList[process.env.NODE_ENV]
	let alarmMsgInstance = null
	// #ifdef APP-PLUS
	const bgAudioManager = uni.getBackgroundAudioManager()
	// #endif
	uni.$baseUrl = baseUrl
	uni.$liveUrl = liveUrl

	socket.on('connect', () => {
		const {
			id
		} = socket;
		store.commit('chatID', id)
	})
	socket.on('online', (data) => {
		store.commit('users', data.users)
	})
	socket.on('error', (msg) => {
		console.log('ws error', msg)
	})
	//监听全局消息通知
	socket.on('receive', (data) => {
		let arr = [...store.state.chatRecords, data]
		store.dispatch('chatRecords', arr)
		uni.setTabBarBadge({ //显示红点
			index: 3,
			text: arr.length + ''
		})
	})
	//之后做全局保持通话同意
	socket.on('onAccept', (data) => {

	})
	//监听背景音关闭
	uni.$on('offPlyer', () => {
		bgAudioManager.stop()
	})
	//同意
	socket.on('onRefuse', (data) => {
		let info = {
			type: 3, //0文字 1图片 2音频 3视频
			userid: data.receiveid, //自己
			username: data.receiveName,
			receiveid: data.userid, //
			receiveName: data.username, //别人
			receiveImg: data.handimg,
			handimg: data.receiveImg
		}
		// #ifdef H5
		confirm(data, false)
		// #endif
		// #ifdef APP-PLUS
		if (uni.$showModalhide) {
			uni.$showModalhide()
		}
		bgAudioManager.stop()
		// #endif
	})
	//通话弹窗
	socket.on('confirm', (data) => {
		let info = {
			type: 3, //0文字 1图片 2音频 3视频
			userid: data.receiveid, //自己
			username: data.receiveName,
			receiveid: data.userid, //
			receiveName: data.username, //别人
			receiveImg: data.handimg,
			handimg: data.receiveImg
		}
		// #ifdef H5
		confirm(info, true)
		// #endif
		// #ifdef APP-PLUS
		Audioplay()
		let routes = getCurrentPages()
		let curRoute = routes[routes.length - 1].route
		if(curRoute != 'pages_new/chat/index' || 'pages_home/live/live'
		|| 'pages_home/live/index' || 'pages_new/crowdChat/index'
		) {
			appConfirm(info)
		}
		// #endif
	})
	const Audioplay = () => {
		if (!bgAudioManager.src) {
			bgAudioManager.src = `${baseUrl}/喻言 - 蔷薇之巅.ogg`
		} else {
			bgAudioManager.play()
		}
	}
	const appConfirm = (data) => {
		uni.$showModal({
			content: '邀请你视频通话',
			delCancel: false, //不显示取消按钮
			confirmVal: '接听', // 确定
			cancelVal: '挂断', // 取消
			userinfo: data, //信息
			top: 44, //内容离顶部距离
			btnTop: 64, //按钮离顶部距离
			cancelColor: 'rgb(235, 69, 73)',
			confirmColor: 'rgb(2, 212, 3)'
		}).then(res => {
			bgAudioManager.stop()
			data.palyerType = 3
			socket.emit('onAccept', data)
			store.commit('chatUser', {
				userid: data.receiveid,
				username: data.receiveName,
				handimg: data.receiveImg
			})
			goPlay(3, true)
		}).catch(res => {
			let routes = getCurrentPages()
			let curRoute = routes[routes.length - 1].route
			if (curRoute == 'pages_home/live/index') {
				uni.navigateBack()
			}
			socket.emit('onRefuse', data)
			bgAudioManager.stop()
		});

	}
	const goPlay = (palyerType, isAccept) => {
		// #ifdef APP-PLUS
		uni.navigateTo({
			url: `/pages_home/live/index2?palyerType=${palyerType}&isAccept=${isAccept}`
		})
		// #endif
		// #ifndef APP-PLUS
		uni.navigateTo({
			url: `/pages_home/live/live?palyerType=${palyerType}&isAccept=${isAccept}`
		})
		// #endif
	}
	store.commit('$socket', socket)
	//全局音视频弹窗组件
	const confirm = (data, show) => {
		chatCard({
			isShow: show,
			duration: 1500,
			userinfo: data
		}).then(() => {
			data.palyerType = 3
			socket.emit('onAccept', data)
			store.commit('chatUser', {
				userid: data.receiveid,
				username: data.receiveName,
				handimg: data.receiveImg
			})
			goPlay(3, true)
		}).catch(e => {
			let routes = getCurrentPages()
			let curRoute = routes[routes.length - 1].route
			if (curRoute == 'pages_home/live/live') {
				uni.navigateBack()
			}
			socket.emit('onRefuse', data)
		})
	}
</script>

<style lang="scss">
	@font-face {
		font-family: "AlimamaDaoLiTi";
		// src: url('~@/static/font-family/AlimamaDaoLiTi.ttf');//刀率体
		// src: url('~@/static/font-family/AlimamaFangYuanTiVF-Thin.ttf');//方圆
		// src: url('~@/static/font-family/DingTalk_JinBuTi_Regular.ttf');//钉钉进步
		src: url('~@/static/font-family/Alibaba_PuHuiTi_2.0_55_Regular_55_Regular.ttf');//普惠体
		font-weight: 400;
	}

	@import "./uni_modules/vk-uview-ui/index.scss";
	@import "./static/css/style.scss";

	* {
		font-family: 'AlimamaDaoLiTi',
	}

	/*每个页面公共css */
</style>